﻿using log4net;
using Mate.Caching;
using Mate.Core.Base;
using Mate.Core.Configuration;
using Mate.Dto.Merchant;
using Mate.Framework.BaseController.Api;
using Mate.Merchant.Api.Controller.Models.Merchant;
using Mate.Merchant.Service;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace Mate.Merchant.Api.Controller.Controller
{
    public class MerchantController: MerchantBaseController
    {
        private readonly ILog log;
        private readonly ICacheManager cache;
        private readonly MateConfig mateConfig;
        private readonly IMerchantService merchantService;
        private readonly IMerchantCategoryService merchantCategoryService;

        public MerchantController(ILog log, ICacheManager cache, MateConfig mateConfig, IMerchantService merchantService, IMerchantCategoryService merchantCategoryService)
        {
            this.log = log;
            this.cache = cache;
            this.mateConfig = mateConfig;
            this.merchantService = merchantService;
            this.merchantCategoryService = merchantCategoryService;
        }

        #region 商户筛选
        /// <summary>
        /// 商户筛选
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public BasePageResult<IList<MerchantSearchDto>> Search(MerchantSearchRequest req)
        {
            BasePageResult<IList<MerchantSearchDto>> result = new BasePageResult<IList<MerchantSearchDto>>();
            if (req.merchantcategory == 0)
            {
                result.ResultCode = ResultCode.ParamError;
                result.Message = "参数错误";
                return result;
            }
            int recordCount=0;

            var merchantcategory = merchantCategoryService.GetCurrentMerchantCategoryAndChildren(req.merchantcategory).Select(x=>x.id);

            var dataList = merchantService.MerchantSearch(req.PageIndex, req.PageSize, merchantcategory, req.is_hot, req.merchantname
                , req.sortType, req.isNew, req.isBrand, ref recordCount);
            if (dataList == null)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "暂无相关数据";
                return result;
            }
            result.ResultCode = ResultCode.Success;
            result.Message = "OK";
            result.PageSize = req.PageSize;
            result.PageIndex = req.PageIndex;
            result.TotalCount = recordCount;
            result.Data = dataList;
            return result;
        }
        #endregion

        #region 获取商户详情
        /// <summary>
        /// 获取商户详情
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResult<MerchantDetailDto> Detail(MerchantDetailRequest req)
        {
            BaseResult<MerchantDetailDto> result = new BaseResult<MerchantDetailDto>();
            if (req.merchantId == 0)
            {
                result.ResultCode = ResultCode.ParamError;
                result.Message = "参数错误";
                return result;
            }
            var merchantModel = merchantService.MerchantDetail(req.merchantId);
            if (merchantModel != null)
            {
                result.ResultCode = ResultCode.Success;
                result.Message ="获取商户信息成功";
                result.Data = merchantModel;
            }
            else
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "获取商户信息失败";
            }
            return result;
        }
        #endregion



    }
}
